﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports OMANE.Modules.Indicateurs.Facades

Namespace Views

    Partial Public Class ViewDashboard
        Public Sub New()
            MyBase.New()

            Me.InitializeComponent()

            ' Insert code required on object creation below this point.
        End Sub


        Private Sub ViewDashboard_DataContextChanged(ByVal sender As Object, ByVal e As System.Windows.DependencyPropertyChangedEventArgs) Handles Me.DataContextChanged
            Dim def = TryCast(e.NewValue, VMDashboard)

            If def Is Nothing Then
                mainGrd.Children.Clear()
                Return
            End If

            Dim o = 1
            If def.Definition.LayoutDefinition.ColumnCount > mainGrd.ColumnDefinitions.Count Then
                For i As Integer = mainGrd.ColumnDefinitions.Count To def.Definition.LayoutDefinition.ColumnCount - 1
                    mainGrd.ColumnDefinitions.Add(New ColumnDefinition())
                Next
            ElseIf def.Definition.LayoutDefinition.ColumnCount < mainGrd.ColumnDefinitions.Count Then
                mainGrd.ColumnDefinitions.RemoveRange( _
                    def.Definition.LayoutDefinition.ColumnCount, _
                   mainGrd.ColumnDefinitions.Count - def.Definition.LayoutDefinition.ColumnCount)
            End If

            If def.Definition.LayoutDefinition.RowCount > mainGrd.RowDefinitions.Count Then
                For i As Integer = mainGrd.RowDefinitions.Count To def.Definition.LayoutDefinition.RowCount - 1
                    mainGrd.RowDefinitions.Add(New RowDefinition())
                Next
            ElseIf def.Definition.LayoutDefinition.RowCount < mainGrd.RowDefinitions.Count Then
                mainGrd.RowDefinitions.RemoveRange( _
                    def.Definition.LayoutDefinition.RowCount, _
                   mainGrd.RowDefinitions.Count - def.Definition.LayoutDefinition.RowCount)
            End If

            For Each c In mainGrd.Children
                Dim bd = TryCast(c, Border)
                If bd IsNot Nothing Then
                    Dim cc = TryCast(bd.Child, ContentControl)
                    If cc IsNot Nothing Then
                        cc.Content = Nothing
                    End If
                End If
            Next
            mainGrd.Children.Clear()
            For Each z In def.Definition.Zones
                Dim brd = New Border()
                Dim facade = New EditZoneFacade(z.prebuildeditzone)

                brd.Background = facade.Background
                brd.BorderBrush = facade.BorderBrush
                brd.BorderThickness = New Thickness(facade.BorderSize)
                brd.CornerRadius = New CornerRadius(facade.CornerRadius)
                brd.Width = facade.Width
                brd.Height = facade.Height
                brd.HorizontalAlignment = facade.HorizontalAlignment
                brd.VerticalAlignment = facade.VerticalAlignment
                brd.Margin = facade.Margin

                Grid.SetColumn(brd, z.StartColumn)
                Grid.SetColumnSpan(brd, z.ColumnCount)
                Grid.SetRow(brd, z.StartRow)
                Grid.SetRowSpan(brd, z.RowCount)

                Dim instfacade = TryCast(z.GraphDefinition.DetailInstance, UIElement)
                If instfacade Is Nothing Then
                    brd.Child = New ContentControl() With {.Content = z.GraphDefinition.DetailInstance}
                    '  Dim bd = New Binding("DataContext")
                    ' BindingOperations.SetBinding(brd.Child, ContentControl.ContentProperty, New Binding())
                Else
                    Dim oo = 1
                    Dim targetfacade As IFacade = z.GraphDefinition.DetailEdit.target

                    Dim clonefacade As IFacade = targetfacade.Clone()

                    brd.Child = clonefacade.Brother
                End If
                mainGrd.Children.Add(brd)
            Next

            mainGrd.ShowGridLines = False
        End Sub
    End Class
End Namespace
